#pragma once
#include <strings.h>
#include "InetAddr.hpp"
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory>
#include <sys/socket.h>
#include "Log.hpp"
#include "Common.hpp"
// 基类
// 规定创建套接字的方法
// 提供一个或者是多个·固定模式下 socket的方法
namespace socketModule
{
    using namespace LogModule;

    class Socket
    {
    public:
        virtual ~Socket() = default;
        virtual void socketOrDie() = 0;
        virtual bool bindOrDie(int port) = 0;
        virtual bool listenOrDie() = 0;
        virtual void setSocketOpt() = 0;
        virtual std::shared_ptr<Socket> accepter(InetAddr *client) = 0;
        virtual void close() = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &in) = 0;
        virtual int fd() = 0;

        // 还需要其他的方法时，再加

        void bulidTcpSocketMethod(int port)
        {
            socketOrDie();
            setSocketOpt();
            bindOrDie(port);
            listenOrDie();
        }
    };
    using sockptr = std::shared_ptr<Socket>;

    class tcpSocket : public Socket
    {
    public:
        tcpSocket(int socket = gdefaultsock) : _sockfd(socket)
        {
        }
        virtual ~tcpSocket() override
        {
        }
        virtual void socketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::ERROR) << "socket error";
                exit(SOCKET_ERR);
            }
            LOG(LogLevel::INFO) << "socket create success";
        }
        virtual bool bindOrDie(int port) override
        {
            if (_sockfd == gdefaultsock)
                return false;
            InetAddr addr(port);

            int n = ::bind(_sockfd, addr.NetAddr(), addr.NetAddrLen());
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "bind error";
                exit(BIND_ERR);
            }
            LOG(LogLevel::DEBUG) << "bind create success: " << _sockfd;
            return true;
        }
        virtual bool listenOrDie() override
        {
            if (_sockfd == gdefaultsock)
                return false;
            int n = ::listen(_sockfd, gbacklog);
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "listen error";
                exit(LISTEN_ERR);
            }
            LOG(LogLevel::DEBUG) << "listen create success: " << _sockfd;
            return true;
        }
        virtual void setSocketOpt()
        {
            int opt = 1;
            // zdl:: 保证我们的分局武器在异常断开的时候， 可以立即被使用
            int n = ::setsockopt(_sockfd, SOL_SOCKET, 
                SO_REUSEADDR, &opt, sizeof opt);
        }
        virtual sockptr accepter(InetAddr *client) override
        {
            if (!client)
                return nullptr;
            struct sockaddr_in peer;
            int newsockfd = ::accept(_sockfd, CONV(&peer), std::make_shared<socklen_t>(sizeof(peer)).get());
            if (newsockfd < 0)
            {
                LOG(LogLevel::WARNING) << "accept error";
                return nullptr;
            }
            client->setAddr(std::move(peer), sizeof(peer));
            return std::make_shared<tcpSocket>(newsockfd);
        }
        virtual void close() override
        {
            if (_sockfd == gdefaultsock)
                return;
            ::close(_sockfd);
        }
        virtual int Recv(std::string *out) override
        {
            char buffer[1024 * 1024];
            auto size = ::recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if (size > 0)
            {
                buffer[size] = 0;
                *out = buffer;
            }

            return size;
        }
        virtual int Send(const std::string &in) override
        {
            return ::send(_sockfd, in.c_str(), in.size(), 0);
        }
        virtual int fd() override { return _sockfd; }

    private:
        int _sockfd;
    };

}
